//
//  PileListView.swift
//  lovelyWeather
//
//  Created by MAC on 2019/7/31.
//  Copyright © 2019 zk. All rights reserved.
//

import UIKit

let color1 = rgr(r: 227, g: 187, b: 136)
let color2 = rgr(r: 220, g: 151, b: 100)
let color3 = rgr(r: 177, g: 105, b: 91)
let color4 = rgr(r: 100, g: 71, b: 73)

func rgr(r:CGFloat,g:CGFloat,b:CGFloat) -> UIColor {
    return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
}

class PileListView: UIScrollView,UIScrollViewDelegate {

    
    lazy var card1 = WeatherCardView.init(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 250), bgColor: color1 , lineColor: color2)
    lazy var card2 = WeatherCardView(frame: CGRect(x: 0, y: 100, width: kScreenW, height: 250), bgColor: color2, lineColor: color3)
    lazy var card3 = WeatherCardView(frame: CGRect(x: 0, y: 200, width: kScreenW, height: 250), bgColor: color3, lineColor: color4)
    lazy var card4 = WeatherCardView(frame: CGRect(x: 0, y: 300, width: kScreenW, height: 250), bgColor: color4, lineColor: color1)

    private lazy var contentView: UIView = UIView.init(frame: self.bounds)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.contentView =
        self.addSubview(contentView)
//        decelerationRate = UIScrollView.DecelerationRate(rawValue: 0.000001)
        self.contentView.addSubview(card1)
        self.contentView.addSubview(card2)
        self.contentView.addSubview(card3)
        self.contentView.addSubview(card4)

        self.contentSize = CGSize(width: self.bounds.width, height: self.bounds.height + 3 * 150)
        self.contentOffset = CGPoint(x: 0, y: 450)
      
        
    }
    
    func configData(_ array:[infoModel]) {
        card1.data = array[0]
        card2.data = array[1]
        card3.data = array[2]
        card4.data = array[3]
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
        super.setContentOffset(contentOffset, animated: animated)
    }
    
    override var contentOffset: CGPoint {
        set {
            self.contentView.frame = CGRect(x: 0, y: newValue.y, width: self.bounds.width, height: self.bounds.height)
            self.updateLayoutOfCard(contentOffset: newValue)
            super.contentOffset = newValue
//            print(newValue.y)
        }
        get {
            return super.contentOffset
        }
    }
    
   

    func updateLayoutOfCard(contentOffset: CGPoint) {
        let y = contentOffset.y
        
        let raiseRate: CGFloat = 100 / 150.0
        let downRate: CGFloat = 150.0/100.0
        if y >= 450 {
            self.card4.y = 300
            self.card3.y = 200
            self.card2.y = 100
            self.card1.y = 0
            
            self.card4.weatherStatusLayer.y = 25
             self.card3.weatherStatusLayer.y = 125
             self.card2.weatherStatusLayer.y = 125
             self.card1.weatherStatusLayer.y = 125
        } else if y >= 300 {
            self.card4.y = 300 + 450 - y
            self.card3.y = 200
            
            
             let dx = (450.0 - y) * downRate
            self.card4.weatherStatusLayer.y = 25 - dx
            self.card3.weatherStatusLayer.y = 125 - ((450.0 - y) * raiseRate)
            
            self.card2.y = 100
            self.card1.y = 0
        }else if y >= 150 {
            self.card4.y = 450
            self.card3.y = 200 + 300 - y
            
            self.card4.weatherStatusLayer.y = -200
            
            let dx = (300.0 - y) * downRate
            self.card3.weatherStatusLayer.y = 25 - dx
            self.card2.weatherStatusLayer.y = 125 - ((300.0 - y) * raiseRate)
            
            
            self.card2.y = 100
            self.card1.y = 0
        }else  {
            self.card4.y = 450
            self.card3.y = 350
            
            if y < 0 {
                self.card2.y = 100 + 150
            }else {
                self.card2.y = 100 + 150 - y
            }
            self.card3.weatherStatusLayer.y = -200
            let dx = (150.0 - y) * downRate
            self.card2.weatherStatusLayer.y = 25 - dx
            self.card1.weatherStatusLayer.y = 125 - ((150.0 - y) * raiseRate)
            
            
            self.card1.y = 0
        }
    }
    

}

extension UIView {
    
    var y: CGFloat {
        set {
            self.frame = CGRect(x: self.frame.minX, y: newValue, width: self.frame.width, height: self.frame.height)
        }
        get {
            return self.frame.minY
        }
    }
}

extension CALayer {
    
    var y: CGFloat {
        set {
            self.frame = CGRect(x: self.frame.minX, y: newValue, width: self.frame.width, height: self.frame.height)
        }
        get {
            return self.frame.minY
        }
    }
}
